﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KTV
{
    class Sqlyuju
    {
        string conStr = "Data Source=172.16.1.45;Initial Catalog=DB_A2011_G02;Persist Security Info=True;User ID=a2011;Password=Abcd_!23^";
        SqlConnection conn = null;

        /// <summary>
        /// data显示
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>null为不成功</returns>
        public DataSet GetData(string sql)
        {
            try
            {
                conn = new SqlConnection(conStr);
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, conn);
                DataSet dataSet = new DataSet();
                sqlDataAdapter.Fill(dataSet);
                return dataSet;
            }
            catch
            {
                return null;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        /// <summary>
        /// 增删改返回
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns>-1返回不成功</returns>
        public int ExeInDeUp(string sql)
        {
            try
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                return comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return -1;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
        
    }
}
